package com.cnplan.controller.auth;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cnplan.core.Constant;
import com.cnplan.core.MyUtils;
import com.cnplan.core.ResultGenerator;
import com.cnplan.entity.SystemRole;
import com.cnplan.service.AuthService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Administrator on 2018/5/2.
 */
@Controller
@RequestMapping("/auth/role")
public class RoleController {

    @Autowired
    private AuthService authService;

    // list role
    @RequestMapping(value={"/list"}, method = RequestMethod.GET)
    public String list(Integer pageIndex, Integer pageSize, String keyword, Model model, HttpServletRequest request){
        if(pageIndex==null || pageIndex<1){
            pageIndex= 1;
        }
        if(pageSize==null || pageSize<0){
            pageSize = Constant.PAGE_SIZE;
        }

        PageHelper.startPage(pageIndex, pageSize);
        List<SystemRole> systemRoleList = authService.listSystemRole(new EntityWrapper<SystemRole>(), keyword);
        PageInfo<SystemRole> pageInfo = new PageInfo<SystemRole>(systemRoleList);

        model.addAttribute("title", "角色");
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("keyword", keyword);
        model.addAttribute("recordList", systemRoleList);

        return "auth/role/list";
    }

    // list role (post)
    @RequestMapping(value = {"/list"}, method = RequestMethod.POST)
    public String listPost(String keyword, Model model, HttpServletRequest request){
        keyword = MyUtils.formatKeyword(keyword);
        model.addAttribute("keyword", keyword);

        return "redirect:" + request.getServletPath();
    }

    // create role
    @RequestMapping(value = {"/create"}, method = RequestMethod.POST)
    @ResponseBody
    public String createPost(ServletRequest request){
        Gson gson = new Gson();
        try{
            SystemRole systemRole = new SystemRole();
            systemRole.setRoleName("Role_" + System.currentTimeMillis());
            systemRole.setRoleDesc("");
            systemRole.setDisplayOrder(1);
            systemRole.setActive(true);

            return gson.toJson(authService.insertSystemRole(systemRole));
        }catch (Exception e){
            return gson.toJson(ResultGenerator.genFailResult("新增角色异常 => " + e.getMessage()));
        }
    }

    // update role
    @RequestMapping(value = {"/update/{id}"}, method = RequestMethod.GET)
    public String update(@PathVariable("id") Long id, Model model){
        SystemRole tempRole = new SystemRole();
        tempRole.setId(id);
        SystemRole systemRole = authService.getSystemRole(tempRole);

        model.addAttribute("title", "角色");
        model.addAttribute("record", systemRole);
        return "auth/role/update";
    }

    // update role (post)
    @RequestMapping(value = {"/update"}, method = RequestMethod.POST)
    @ResponseBody
    public String updatePost(SystemRole systemRole){
        Gson gson = new Gson();
        try{
            return gson.toJson(authService.updateSystemRole(systemRole));
        } catch (Exception e){
            return gson.toJson(ResultGenerator.genFailResult("更新角色异常 => " + e.getMessage()));
        }
    }

    // delete role (post)
    @RequestMapping(value = {"/delete"}, method = RequestMethod.POST)
    @ResponseBody
    public String deletePost(Long id){
        Gson gson = new Gson();
        try{
            return gson.toJson(authService.deleteSystemRole(id));
        } catch (Exception e){
            return gson.toJson(ResultGenerator.genFailResult("删除角色异常 => " + e.getMessage()));
        }
    }
}
